<?php

/**
 * Created by PhpStorm.
 * User: JS
 * Date: 2019/2/13
 * Time: 13:51
 */
require_once 'IP360Client.php';
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Cache\Simple\FilesystemCache;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
class IPLookupService
{
    public $config;
    public $client;
    public function __construct()
    {
        $this->config = require_once ('config.php');
    }


    /*
     *
     */
    public function lookup($ip)
    {
        $clientName = $this->config['iplookup']['name'];
        $cache = $this->config['cache'];
        $log =  $this->config['log'];
        if(class_exists($clientName))//判断类是否存在
        {
            $filecache = new FilesystemCache('',$cache['time'],$cache['dir']);
            $this->client =  new $clientName($filecache);
            $logger  =  new  Logger(' my_logger');
            $logger->pushHandler(new  StreamHandler($log['filepath']));
//            $logger->pushHandler(new  FirePHPHandler());
            $place =   $this->client->lookup($ip);
            $logger->info('ip:'.$ip,['place'=>$place]);
            return $place;
        }else{
            return "配置错误";
        }
    }
}



